package com.gf.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gf.bean.User;
import com.gf.service.impl.UserServiceImpl;
import com.gf.utils.Const;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/*
 * 添加用户的地址和电话号码servlet
 * */

@WebServlet(name = "AdduserroomandtelServlet",urlPatterns ="/adduserroomandtel" )
public class AdduserroomandtelServlet extends HttpServlet {
    static UserServiceImpl usermapper ;
    static  {
        usermapper=new UserServiceImpl();

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Pragma", "No-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");

        int  userid= (int) request.getSession().getAttribute("userid");
        //提取用户的用户id
        String room_number = request.getParameter("room_number");
        String telephone = request.getParameter("telephone");
        //接收用户传过来的地址和电话
        User user = new User(userid,room_number,telephone);
        //将数据封装进对象
        JSONObject backjson = new JSONObject();
        //新建json对象用于返回数据
        int updateuserroomandtel = usermapper.updateuserroomandtel(user);
        if (updateuserroomandtel==1){
            //将之前封装好的对象作为参数传递过去
            response.setStatus(200);

            backjson.put("state", 200);
            backjson.put("message", Const.ADDDATA_SUCESS);
        }else{
            response.setStatus(300);

            backjson.put("state",300);
            backjson.put("message", Const.ADDDATA_FAIL);

        }
        PrintWriter out = response.getWriter();
        out.println(JSON.toJSON(backjson));
        //将JSON对象转化为字符串输出
        out.flush();
        out.close();


    }
}
